<?php

/**
 * Created by PhpStorm.
 * 
 */
class ServerApplyController extends AdminBaseController {

    public function index(){
		$this->setTitle('服务器申请列表');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addFilter('server_name', 'input', '服务器名称')
            ->addFilter('status', 'select', '服务器类型', array('value' => '', 'options' => array('' => 'ALL') + ServerService::statusList()));
		$filter->addSpecial(true);
		
		$statusList = SendService::status();
		$statusList = array('' => '所有') + $statusList;
		$filter->addFilter('apply_status', 'select', '状态', array(
				'options'   => $statusList,
				'value'     => key($statusList)
		));
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = ServerApplyService::indexHeader();
		if(PubFunc::isAjax()){
			$result = ServerApplyService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=serverApply/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'serverApply/index',
					'length'=>50,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}
	
	public function approveList(){
		$this->setTitle('服务器审核列表');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addFilter('server_name', 'input', '服务器名称')
		->addFilter('status', 'select', '服务器类型', array('value' => '', 'options' => array('' => 'ALL') + ServerService::statusList()));
		$filter->addSpecial(true);
	
		$statusList = SendService::status();
		$filter->addFilter('apply_status', 'select', '状态', array(
				'options'   => $statusList,
				'value'     => key($statusList)
		));
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = ServerApplyService::indexHeader();
		if(PubFunc::isAjax()){
			$result = ServerApplyService::approveData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=serverApply/approveList", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'serverApply/approveList',
					'length'=>50,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}

    public function add(){
        $this->setTitle('服务器申请');

        //$this->out['statusList'] = ServerApplyService::statusList();
        $this->out['statusList'] = ServerService::checkFlagList();
        
        $specialList = SpecialServerService::specialList();
        $specialList = array(''=>'请选择') + $specialList;
        $this->out['specialList'] = $specialList;
    }

    public function doAdd(){
        $result = ServerApplyService::doAdd($_POST);

        if($result !== false){
            $this->showMessage('新增成功', PubFunc::buildUrl("/oss/?r=serverApply/index"));
        }else{
            $this->showMessage('新增失败');
        }
    }
    
    public function apply(){
    	$id = $this->R('id');
    	if(PubFunc::isAjax()){
    		$data = array();
    		$data['id'] = $id;
    		$data['special_id'] = $this->R('special_id');
    		$data['channel_list'] = $this->R('channel');
    		$data['server_name'] = $this->R('server_name');
    		$data['server_num'] = 0;
    		$data['status'] = $this->R('status');
    		$data['open_time'] = $this->R('open_time');
    		$result = ServerApplyService::apply($data);
    		$this->out = array(
    				'status' => 1,
    				'msg' => $result,
    				'data'    => ''
    		);
    		$this->outType = 'json';
    	}else{
    	    $info = ServerApplyService::get($id);
    	
    	    if(empty($info)) $this->showMessage('找不到该信息');
    	    $this->out['info'] = $info;
    	
    	    $this->out['channes'] = $info['channel_list'];
    
    	    //$this->out['statusList'] = ServerApplyService::statusList();
    	    $this->out['statusList'] = ServerService::checkFlagList();
    
    	    $specialList = SpecialServerService::specialList();
    	    $specialList = array(''=>'请选择') + $specialList;
    	    $this->out['specialList'] = $specialList;
    	    $this->layout = '';
    	}
    }
    

    public function delete(){
        $id = $this->R('id');

        $result = ServerApplyService::delete($id);

        if($result !== false){
            $this->showMessage('删除成功', PubFunc::buildUrl("/oss/?r=serverApply/index"));
        }else{
            $this->showMessage('删除失败');
        }
    }
    
    /**
     * 查看审批信息
     */
    public function getApproval(){
    	$this->layout = '';
    	$id = PubRequest::R('id');
    
    	$result = ServerApplyService::getApproval($id);
    	if(!$result){
    		$result = array();
    	}
    
    	$this->out['list']      = $result;
    }
    
    /**
     * 驳回
     */
    public function reject(){
    	$id = $this->R('id');
    
    	$result = ServerApplyService::reject($id);
    
    	$this->out = $result;
    	$this->outType = 'json';
    }
    
    /**
     * 批准
     */
    public function approve(){
    	$id = $this->R('id');
    
    	$result = ServerApplyService::approve($id);
    
    	$this->out = $result;
    	$this->outType = 'json';
    }
    

}